<template>
  <div>
    <el-drawer
      title="调整余额"
      :before-close="handleClose"
      :visible="teamUpdateBlanceDialog"
      direction="rtl"
      custom-class="demo-drawer"
      ref="drawer"
      size="60%"
    >
      <div class="demo-drawer__content">
        <el-form
          ref="formInfoRef"
          :model="formInfo"
          :rules="formRules"
          label-position="top"
        >
          <el-form-item label="归属油站" prop="stationId">
            <el-select
              v-model="formInfo.stationId"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option
                v-for="(item, index) in stationList"
                ::key="index"
                :label="item.stationName"
                :value="item.stationId"
                style="width: 100%"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="调整余额" prop="rechargeAmount">
            <h6 class="amtText">
              当前余额(元) <br />
              {{ currentData.balance }}
            </h6>
            <div class="div1">
              <el-select v-model="formInfo.cardType" style="width: 100px">
                <el-option label="增加" value="imp"></el-option>
                <el-option label="扣减" value="msub"></el-option>
              </el-select>
              <el-input
                v-model="formInfo.rechargeAmount"
                placeholder="请输入调整余额"
              ></el-input>
            </div>
            <h6 class="amtText">调整后余额(元) <br />{{ affterAmt }}</h6>
          </el-form-item>

          <el-form-item label="是否推送消息" prop="isPush">
            <el-radio-group v-model="formInfo.isPush">
              <el-radio label="0">否</el-radio>
              <el-radio label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="备注" prop="notes">
            <el-input
              type="textarea"
              v-model="formInfo.notes"
              placeholder="请输入备注"
              maxlength="30"
              show-word-limit
            />
          </el-form-item>
        </el-form>

        <div class="demo-drawer__footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="submitForm" :loading="loading">
            {{ loading ? "提交中 ..." : "确 定" }}
          </el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>
<script>
import { listEnterpriseStation } from "@/api/dept";
import { updateTeamCardBlanceApi } from "@/api/teamCard/teamCardList";
export default {
  name: "TeamUpdateBlance",
  props: {
    teamUpdateBlanceDialog: {
      type: Boolean,
      default: false,
    },
    currentData: {
      type: Object,
      default: () => {},
    },
  },
  computed: {
    affterAmt() {
      let currentAmt = this.currentData.balance;
      let amt = this.formInfo.rechargeAmount;
      if (!amt) {
        return currentAmt;
      } else if (this.formInfo.cardType === "imp") {
        return Number(currentAmt) + Number(amt);
      } else if (this.formInfo.cardType === "msub") {
        return Number(currentAmt) - Number(amt);
      }
    },
  },
  watch: {
    teamUpdateBlanceDialog(val) {
      if (val) {
        this.getStationList();
      }
    },
  },

  data() {
    // 金额
    let checkrechargeAmount = (rule, value, callback) => {
      if (!value) return callback(new Error("请输入金额"));
      let amt = Number(value.trim());
      const pattern = /^\d+(\.\d{1,2})?$/;
      console.log("校验金额：", value, typeof value, amt);
      if (amt < 0) return callback(new Error("不能小于0"));
      if (value.length > 11) return callback(new Error("最多11位"));
      if (Number.isNaN(amt)) return callback(new Error("请输入有效值"));
      if (value.startsWith(".")) return callback(new Error("请输入有效值"));
      if (!pattern.test(amt)) return callback(new Error("最多两位小数点"));

      let cardType = this.formInfo.cardType;
      if (cardType === "msub" && this.affterAmt < 0) {
        return callback(new Error("扣减金额不能大于当前余额"));
      }
      callback();
    };
    return {
      formInfo: {
        stationId: "",
        cardType: "imp",
        rechargeAmount: "",
        isPush: "0",
        notes: "",
      },
      loading: false,
      stationList: [],
      formRules: {
        stationId: [
          { required: true, message: "请选择油站", trigger: "change" },
        ],
        rechargeAmount: [
          { required: true, validator: checkrechargeAmount, trigger: "blur" },
        ],
        isPush: [{ required: true, message: "请选择", trigger: "blur" }],
      },
    };
  },
  methods: {
    handleClose() {
      this.$emit("update:teamUpdateBlanceDialog", false);
      this.resetForm("formInfoRef");
      this.formInfo.cardType = "imp";
    },

    // 查询油站列表
    async getStationList() {
      let enterpriseId = this.presentEnterprise.enterpriseId;
      const params = {
        enterpriseId: this.presentEnterprise.enterpriseId,
        status: 1,
      };
      const res = await listEnterpriseStation(params);
      this.stationList = res;
      // 只有一个站时，默认选中
      if (res.length === 1) {
        this.formInfo.stationId = res[0].stationId;
      }
    },

    // 提交
    submitForm() {
      this.$refs.formInfoRef.validate(async (valid) => {
        if (valid) {
          const params = { ...this.formInfo };
          params.fleetCardId = this.currentData.id;
          params.rechargeAmount = Number(this.formInfo.rechargeAmount.trim());
          await updateTeamCardBlanceApi(params);
          this.msgSuccess("余额调整成功");
          this.handleClose();
          this.$emit("reloadData");
        } else {
          console.log("error submit");
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .demo-drawer {
  overflow: auto;
  .el-drawer__header {
    border-bottom: 1px solid #ddd;
    padding-bottom: 15px;
  }
  .el-drawer__body {
    padding: 20px;
    .amtText {
      line-height: 30px;
    }
  }

  .div1 {
    display: flex;
    align-items: center;

    .el-select {
      margin: 15px 10px 15px 0px;
    }
  }
}
</style>
